package com.culturalCenter.dataCenter.controller.web;

import com.alibaba.fastjson.JSON;
import com.culturalCenter.dataCenter.Utils.ApiResult;
import com.culturalCenter.dataCenter.Utils.ResultCode;
import com.culturalCenter.dataCenter.entity.Wechat.GetCultureTokenParam;
import com.culturalCenter.dataCenter.entity.Wechat.GetFeiYiTokenParam;
import com.culturalCenter.dataCenter.entity.Wechat.WechatFans;
import com.culturalCenter.dataCenter.globalconfig.Interface.ApiVersion;
import com.culturalCenter.dataCenter.globalconfig.Interface.CustomVersion;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.culturalCenter.dataCenter.Utils.ApiResult.instance;

/**
 * @Author Tepid Chan
 * @Date 2022/9/24 16:27
 * 微信公众号粉丝量和文章阅读量
 **/
@RestController
@RequestMapping("/{version}/wechat")
@Api(tags = "公众号模块")
@ApiVersion(CustomVersion.VERSION_1)
@Slf4j
@Deprecated
public class OfficialAccountsController {
    private String getTokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";//&appid="+appid+"&secret="+secret;
    private String getFansUrl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=";
    private String getReadUrl="https://api.weixin.qq.com/datacube/getuserread?access_token=";

    @Resource
    private RestTemplate restTemplate;

    @SneakyThrows
    @GetMapping(value = "/getFans")//,produces = {"application/json"}
    public ApiResult<List<WechatFans>> getFans(){

        GetCultureTokenParam getCultureTokenParam = new GetCultureTokenParam();
        //获取token
        String cultureData = restTemplate.getForObject(getTokenUrl+"&appid="+getCultureTokenParam.getAppid()+"&secret="+getCultureTokenParam.getSecret(), String.class);
//        log.info("cultureData:{}",cultureData);
        String cultureAccess_token=JSON.parseObject(cultureData).getString("access_token");
        //获取总粉丝数
        String cultureFansData = restTemplate.getForObject(getFansUrl + cultureAccess_token + "&next_openid=", String.class);
        int cultureFans = JSON.parseObject(cultureFansData).getInteger("total");
//        log.info("cultureFans:{}",cultureFans);
        GetFeiYiTokenParam getFeiYiTokenParam=new GetFeiYiTokenParam();
        //获取token

        String legacyData = restTemplate.getForObject(getTokenUrl+"&appid="+getFeiYiTokenParam.getAppid()+"&secret="+getFeiYiTokenParam.getSecret(), String.class);

//        log.info("legacyData:{}",legacyData);
        String legacyAccess_token=JSON.parseObject(legacyData).getString("access_token");

        //获取总粉丝数
        String legacyFansData = restTemplate.getForObject(getFansUrl + legacyAccess_token + "&next_openid=", String.class);
        int legacyFans = JSON.parseObject(legacyFansData).getInteger("total");
//        log.info("legacyFans:{}",legacyFans);

        List<WechatFans> response = new ArrayList<>();
        WechatFans fans = new WechatFans();
        fans.setCultureFans(cultureFans);
        fans.setLegacyFans(legacyFans);
        response.add(fans);
        //获取阅读量
        /*GetReaderParam getReaderParam = new GetReaderParam();
        getReaderParam.setAccess_token(access_token);
        String readData = restTemplate.postForObject(getReadUrl+access_token, getReaderParam, String.class);
        log.info("readData={}",readData);*/

        return ApiResult.instance(ResultCode.SUCCESS,response);
    }
}
